angular.module('sale-roles.services', [])

/**
 * 销售数据
 */
.factory('SalesService',function(){
  //保存从数据库读取的数据
  var getSaleOrdersSub = function(scope){
    return scope.subscribe('sale_orders');
  };


  return {
    /**
     * 总销售额
     */
    analytic : function(scope,conditions,callback){
      scope.helpers({
        sale_orders: function() {
          return SaleOrders.find(conditions);
        }
      });
      scope.autorun(function() {
        if(getSaleOrdersSub(scope).ready() && scope.sale_orders){
          var anaSale={totalSale:0,orderCount:0,productCount:0,loaded:false};

          if(scope.sale_orders.length){
            //遍历今日订单
            scope.sale_orders.forEach(function(order){
              anaSale.totalSale+=order.amount;
              // anaSale.orderCount++;
              anaSale.productCount+=order.total_count;
            });
          }
          anaSale.orderCount = scope.sale_orders.length;
          anaSale.loaded = true;
          callback(anaSale);
        }
      });
    },
    /**
    获取当月订单数 TODO
    */
    getOrderCountMonthly : function(scope){
      scope.helpers({
        saleOrdersCountMonthly: function() {
          return SaleOrders.find({'date':{"$gt":moment().startOf('month').toDate()}}).count();
        }
      });
    },
    /**
    获取全部订单数 TODO
    */
    getOrderCountAll : function(scope){
      scope.helpers({
        saleOrdersCountAll: function() {
          return SaleOrders.find().count();
        }
      });
    },

    /**
     * 业绩统计
     */
    getPerformance : function(scope,callback){
      scope.helpers({
        sale_orders: function() {
          return SaleOrders.find();
        }
      });
      scope.autorun(function() {
        if(getSaleOrdersSub(scope).ready() && scope.sale_orders){
          //按时间获取销售数据
          var myPreformance={
            todayPref:{
              title:"今日",
              startTime:moment().startOf('day'),endTime:moment(),
              totalSale:0,count:0},
            yesterdayPref:{
              title:"昨日",
              startTime:moment().subtract(1, 'day').startOf('day'),endTime:moment().startOf('day'),
              totalSale:0,count:0},
            thisMonthPref:{
              title:"本月",
              startTime:moment().startOf('month'),endTime:moment(),
              totalSale:0,count:0},
            lastMonthPref:{
              title:"上月",
              startTime:moment().subtract(1, 'month').startOf('month'),endTime:moment().startOf('month'),
              totalSale:0,count:0},
            totalPref:{title:"全部",totalSale:0,count:0}
          };

          if(scope.sale_orders.length){
            scope.sale_orders.forEach(function(order){

              // 本月
              if(order.date>=myPreformance.thisMonthPref.startTime && order.date<myPreformance.thisMonthPref.endTime) {
                myPreformance.thisMonthPref.count++;
                myPreformance.thisMonthPref.totalSale+=order.amount;
                // 今天
                if (order.date>myPreformance.todayPref.startTime) {
                  myPreformance.todayPref.count++;
                  myPreformance.todayPref.totalSale+=order.amount;
                //昨天
                }else if (order.date>=myPreformance.yesterdayPref.startTime && order.date<myPreformance.yesterdayPref.endTime) {
                  myPreformance.yesterdayPref.count++;
                  myPreformance.yesterdayPref.totalSale+=order.amount;
                }
              // 上月
              }else if (order.date>=myPreformance.lastMonthPref.startTime && order.date<myPreformance.lastMonthPref.endTime) {
                myPreformance.lastMonthPref.count++;
                myPreformance.lastMonthPref.totalSale+=order.amount;
                //昨天
                if (order.date>=myPreformance.yesterdayPref.startTime && order.date<myPreformance.yesterdayPref.endTime) {
                 myPreformance.yesterdayPref.count++;
                 myPreformance.yesterdayPref.totalSale+=order.amount;
               }
              }
              // 总计
              myPreformance.totalPref.count++;
              myPreformance.totalPref.totalSale+=order.amount;

            });
          }
          callback(myPreformance);
        }
      });
    },

    /**
     * 销售单列表，按品日期统计
     */
    list : function(scope,fields){
      scope.helpers({
        saleOrdersList: function() {
          return SaleOrders.find(fields);
        }
      });
    }

  };
});
